package cn.test.user.web;

import cn.test.user.api.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @auther hanchao
 * @date 2017/1/23 11:03
 */
@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserService userService;

    //oh yeah loadbalance已经实现了，但是resttemplate虽然够好用了，但是依旧不够灵活
    @RequestMapping("/add")
    @HystrixCommand(fallbackMethod = "addMock")  //利用这个实现ribbon下的断路器，也可以使用feign
    public int add(int a,int b){
        return restTemplate.getForEntity("http://user-service/users/add?a="+a+"&b="+b,Integer.class).getBody();
    }

    public int addMock(int a,int b){
        return 111111;
    }

    @RequestMapping("/add2")
    public int add2(int a,int b){
        return userService.add(a,b);
    }

}
